package com.jian.android.autojipai.app.helper;

import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class FloatingOnTouchListener implements View.OnTouchListener {
    private float initialTouchX;
    private float initialTouchY;
    private int initialX;
    private int initialY;
    private boolean isOnClick;
    private View mFloatingView;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams params;

    public FloatingOnTouchListener(WindowManager.LayoutParams layoutParams, WindowManager windowManager, View view) {
        this.params = layoutParams;
        this.mWindowManager = windowManager;
        this.mFloatingView = view;
    }

    @Override // android.view.View.OnTouchListener
    public boolean onTouch(View view, MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == 0) {
            this.initialX = this.params.x;
            this.initialY = this.params.y;
            this.initialTouchX = motionEvent.getRawX();
            this.initialTouchY = motionEvent.getRawY();
            this.isOnClick = true;
            LogTool.getInstance().printLog("TAG", "按下去");
            return true;
        } else if (action == 1) {
            LogTool.getInstance().printLog("TAG", "抬起来");
            if (this.isOnClick) {
                view.performClick();
            }
            return true;
        } else if (action != 2) {
            return false;
        } else {
            int rawX = (int) (motionEvent.getRawX() - this.initialTouchX);
            int rawY = (int) (motionEvent.getRawY() - this.initialTouchY);
            this.params.x = this.initialX + rawX;
            this.params.y = this.initialY - rawY;
            this.mWindowManager.updateViewLayout(this.mFloatingView, this.params);
            LogTool.getInstance().printLog("TAG", "拖动了");
            if (rawX <= 1 && rawY <= 1) {
                this.isOnClick = true;
            } else {
                this.isOnClick = false;
            }
            return true;
        }
    }
}
